package com.frank.http.encryp;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.SortedMap;
import java.util.TreeMap;

import com.frank.http.encryp.EncrypException;

/**
 * 网络处理签名工具类类
 * 
 * @author Frank
 * @version 1.0 Create by 2016.3.15
 */
public class HttpUtil {
	public static final String CONTENT_TYPE = "Content-Type";
	public static final String CONTENT_LENGTH = "Content-Length";
	public static final String CONTENT_MD5 = "Content-Md5";
	public static final String HOST = "Host";
	public static final String CONTENT_TYPE_CONTENT = "application/x-www-form-urlencoded; charset=UTF-8";
	public static final String CONTENT_TYPE_CONTENT_JSON = "application/json; charset=UTF-8";
	public static final String CONTENT_TYPE_CONTENT_FROM_DATA = "multipart/form-data";

	/**
	 * 获取canonicalURI
	 */
	public static String getCanonicalURI(String uriPath) {
		if (uriPath == null || uriPath.equals("")) {
			return "";
		}
		URI uri = null;
		try {
			uri = new URI(uriPath);
			uriPath = uri.getPath();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return EncrypUtils.getCanonicalURI(uriPath);
	}

	/**
	 * 获取URI
	 */
	public static URI getURI(String uriPath) {
		if (uriPath == null || uriPath.equals("")) {
			return null;
		}
		URI uri = null;
		try {
			uri = new URI(uriPath);
			uriPath = uri.getPath();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return uri;
	}

	/**
	 * 获取HeaderParameters
	 */
	public static SortedMap<String, String> getHeaderParameters(String uriPath,
			ByteArrayOutputStream out, boolean isFormData) throws Exception {
		SortedMap<String, String> headParameters = new TreeMap<String, String>();
		if (out == null) {
			throw new EncrypException("getHeaderParameters():out请求体字节流不能为空");
		}
		if (uriPath == null || uriPath.equals("")) {
			throw new EncrypException("getHeaderParameters():uriPath请求地址不能为空");
		}

		if (out.toByteArray().length != 0) {
			headParameters.put(CONTENT_LENGTH,
					String.valueOf(out.toByteArray().length));
			if (isFormData) {
				headParameters
						.put(CONTENT_TYPE, CONTENT_TYPE_CONTENT_FROM_DATA);
			} else {
				headParameters.put(CONTENT_TYPE, CONTENT_TYPE_CONTENT);
				// headParameters.put(CONTENT_TYPE, CONTENT_TYPE_CONTENT_JSON);
			}
			headParameters.put(CONTENT_MD5, EncrypUtils.get128MD5ToBase64(out));
		}
		headParameters.put(HOST, HttpUtil.getURI(uriPath).getHost());

		return headParameters;
	}

	/**
	 * 获取HeaderParameters
	 */
	public static SortedMap<String, String> getHeaderParameters(String uriPath,
			ByteArrayOutputStream out, boolean isFormData, int type)
			throws Exception {
		SortedMap<String, String> headParameters = new TreeMap<String, String>();
		if (out == null) {
			throw new EncrypException("getHeaderParameters():out请求体字节流不能为空");
		}
		if (uriPath == null || uriPath.equals("")) {
			throw new EncrypException("getHeaderParameters():uriPath请求地址不能为空");
		}

		if (out.toByteArray().length != 0) {
			headParameters.put(CONTENT_LENGTH,
					String.valueOf(out.toByteArray().length));
			if (isFormData) {
				if (type != 1) {
					headParameters.put(CONTENT_TYPE,
							CONTENT_TYPE_CONTENT_FROM_DATA);
				} else {
					headParameters.put(CONTENT_TYPE, CONTENT_TYPE_CONTENT_JSON);
				}
			} else {
				if (type != 1) {
					headParameters.put(CONTENT_TYPE, CONTENT_TYPE_CONTENT);
				} else {
					headParameters.put(CONTENT_TYPE, CONTENT_TYPE_CONTENT_JSON);
				}
			}
			headParameters.put(CONTENT_MD5, EncrypUtils.get128MD5ToBase64(out));
		}
		headParameters.put(HOST, HttpUtil.getURI(uriPath).getHost());

		return headParameters;
	}
}
